package com.campsys.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.campsys.core.TableData;
import com.campsys.domain.TManagerClass;
import com.campsys.domain.param.PageParam;
import com.campsys.domain.vo.ClazzPageVo;
import com.campsys.mapper.ActivityMapper;
import com.campsys.mapper.ClazzMapper;
import com.campsys.mapper.TeacherMapper;
import com.campsys.mapping.ClazzMapping;
import com.campsys.service.IClazzService;
import io.netty.util.internal.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class ClazzServiceImpl extends ServiceImpl<ClazzMapper, TManagerClass> implements IClazzService {

    @Autowired
    private ClazzMapper clazzMapper;

    @Autowired
    private ActivityMapper activityMapper;

    @Autowired
    private TeacherMapper teacherMapper;

    @Override
    public TableData<ClazzPageVo> getClazzPageVo(PageParam pageParam, String name, Long activityId, Long teacherId) {

        LambdaQueryWrapper<TManagerClass> qw = new LambdaQueryWrapper<>();
        qw.like(!StringUtil.isNullOrEmpty(name),TManagerClass::getName,name);
        qw.eq(activityId != null,TManagerClass::getActivityId,activityId);
        qw.eq(teacherId != null,TManagerClass::getTeacherId,teacherId);
        qw.eq(TManagerClass::getStatus,1);

        Page<TManagerClass> page = new Page<>(pageParam.getPageNum(),pageParam.getPageSize());

        Page<TManagerClass> clazzPage = clazzMapper.selectPage(page,qw);

        List<TManagerClass> list1 = clazzPage.getRecords();
        List<ClazzPageVo> list2 = list1.stream()
                .map(tManagerClass -> {
                    ClazzPageVo clazzPageVo = ClazzMapping.INSTANCE.toPage(tManagerClass);
                    clazzPageVo.setTeacherName(teacherMapper.selectById(tManagerClass.getTeacherId()).getTeacherName());
                    clazzPageVo.setActivityName(activityMapper.selectById(tManagerClass.getActivityId()).getName());
                    return clazzPageVo;
                })
                .collect(Collectors.toList());
        return TableData.success(list2,page.getTotal());
    }

    @Transactional
    @Override
    public boolean addStudentInClazz(Long clazzId, List<Long> studentIds) {
        int rs = 0;
        for(Long studentId : studentIds){
            rs = clazzMapper.addStudentInClazz(studentId,clazzId);
        }
        return rs == 1 ? true : false;
    }

    @Override
    public boolean deleteStudentInClazz(Long clazzId, Long studentId) {

        int rs = clazzMapper.deleteStudentInClazz(studentId,clazzId);

        return rs == 1 ? true : false;
    }

    @Override
    public List<Long> getStudentIdsInClazz(Long clazzId) {
        return clazzMapper.getStudentIdsInClazz(clazzId);
    }

    @Override
    public List<Long> getClazzIdsByStudentId(Long studentId) {
        return clazzMapper.getClazzIdsByStudentId(studentId);
    }
}
